'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 修改联盟名字
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionGroupModel = require('../../../../models/unionGroupModel');
var roleModel = require('../../../../models/roleModel.js');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService');
var filterSensitiveService = require('../../../../services/filterSensitiveService');
var constantUtil = require('../../../../util/constantUtil.js');

/**
 * 修改联盟名字
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var name = msg.name;

    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;

    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_group_model = model;
                        cb();
                    });
                } else {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
            },
            function (cb) {

                // 判断否是会长
                if (union_group_model.getUserPosition(role_id) !== 1) {
                    return next(null, {
                        code: Code.UNION_USER_NOT_BOOS
                    });
                }

                // 扣除钻石
                var sub_diamond = constantUtil.value('change_union_name_cost');
                if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_RENAME, sub_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 检测名字是否含有敏感词
                if (!filterSensitiveService.detect(name)) {
                    return next(null, {
                        code: Code.UNION_NAME_CONTAIN_SENSITIVE
                    });
                }

                // 修改公会名字
                union_group_model.setUnionName(name);
                cb();
            }
        ],
        function (cb) {
            // 同步联盟名字到个人角色名
            roleModel.updateUnionName(union_id, name, cb);
        },
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK
            });
        }
    );
};
